package com.sohh.demo;

import java.io.*;
import java.util.Arrays;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @description: 文件压缩 基础
 * @author: ruoan
 * @date: 2021/2/21 15:57
 */
public class Test5 {

    public static void main(String[] args) {
        String path = "d:\\AA.zip";
        String JPG_PATH = "D:\\line.png";
        String FILE_NAME = JPG_PATH.split("\\\\")[1];

        File zipFile  = new File(path);
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile))){
            long beginTime = System.currentTimeMillis();
            for (int i = 0; i < 1; i++) {
                try (InputStream inputStream=new FileInputStream(JPG_PATH)){
                    zipOutputStream.putNextEntry(new ZipEntry(FILE_NAME));
                    int temp = 0;
                    while ((temp=inputStream.read())!=-1) {
                        zipOutputStream.write(temp);
                    }
                }
            }
            long endTime = System.currentTimeMillis();
            System.out.println("花费时间："+(endTime-beginTime)+"ms");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }



}
